package com.qf.controller;

import com.qf.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/message")
public class SendMessage {


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    /**
     * 测试延迟消息
     */
    @GetMapping("/sendDelayed")
    public String sendDelayed() throws Exception{

        //1:
        threadPoolExecutor.execute(() -> {

        });
        //2:
        Future<?> future = threadPoolExecutor.submit(() -> {

        });
        System.out.println(future.get());

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_DELAYED,
                "delayedMessage","我正在测试插件延迟消息",message -> {
                        // 设置延迟的时间  单位是ms
                    message.getMessageProperties().setDelay(8000);
                    System.out.println("开始时间:" + new Date());
                    return message;
                });

        return "OK";

    }
    /**
     * 测试延迟消息
     *     public static final String EXCHANGE_DEAD_LETTER = "exchange_dead_letter";
     *     public static final String QUEUE_DEAD_LETTER = "queue_dead_letter";
     *     public static final String ROUTING_KEY_DEAD_LETTER = "routing_key_dead_letter";
     *     public static final String QUEUE_NORMAL = "queue_normal";
     *     public static final String ROUTING_KEY_NORMAL = "routing_key_normal";
     */
    @GetMapping("/sendDeadLetter")
    public String sendDeadLetter(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_DEAD_LETTER,
                RabbitMQConfig.ROUTING_KEY_NORMAL,"我正在测试死信延迟消息",message -> {
                    // 设置延迟的时间  单位是ms
                    message.getMessageProperties().setExpiration("9000");//基于死信的
                    //message.getMessageProperties().setDelay(8000);//基于插件的
                    System.out.println("开始时间:" + new Date());
                    return message;
                });

        return "OK";

    }
}
